package com.chaofan.sell.service.impl;

import com.chaofan.sell.dto.CartDTO;
import com.chaofan.sell.entity.ProductCategory;
import com.chaofan.sell.entity.ProductInfo;
import com.chaofan.sell.enums.ProductStatus;
import com.chaofan.sell.enums.Result;
import com.chaofan.sell.exception.SellException;
import com.chaofan.sell.repository.ProductCategoryRepository;
import com.chaofan.sell.repository.ProductInfoRepository;
import com.chaofan.sell.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author: Fairy
 * @date: 2021/9/6
 */
@Service
@CacheConfig(cacheNames = "product")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRepository repository;

    @Autowired
    private ProductCategoryRepository categoryRepository;

    @Override
    @Cacheable(key = "123", condition = "#productId != null", unless = "#result.productId == null")
    public ProductInfo findOne(String productId) {
        return repository.findById(productId).orElse(null);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatus.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    @CachePut(key = "123")
    public ProductInfo save(ProductInfo product) {
        return repository.save(product);
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return categoryRepository.findByCategoryTypeIn(categoryTypeList);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (var cartDTO : cartDTOList) {
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).orElse(null);
            if (productInfo == null) {
                throw new SellException(Result.PRODUCT_NOT_EXISTS);
            }
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (var cartDTO : cartDTOList) {
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).orElse(null);
            if (productInfo == null) {
                throw new SellException(Result.PRODUCT_NOT_EXISTS);
            }

            int result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new SellException(Result.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);

            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).orElse(null);
        if (productInfo == null) {
            throw new SellException(Result.PRODUCT_NOT_EXISTS);
        }
        if (productInfo.getProductStatus().equals(ProductStatus.UP.getCode())) {
            throw new SellException(Result.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatus.UP.getCode());
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).orElse(null);
        if (productInfo == null) {
            throw new SellException(Result.PRODUCT_NOT_EXISTS);
        }
        if (productInfo.getProductStatus().equals(ProductStatus.DOWN.getCode())) {
            throw new SellException(Result.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatus.DOWN.getCode());
        return repository.save(productInfo);
    }

}
